import { navigateBack } from './navigateBack';
import { navigateTo } from './navigateTo';
import { redirectTo } from './redirectTo';
import { switchTab } from './switchTab';
const wxp = { navigateTo, redirectTo, switchTab, navigateBack };
const encode = (str) => encodeURIComponent(str).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16)}`).replace(/%2C/g, ',');
const router = (type, location, query = {}, events) => {
    const querystring = Object.keys(query).map((key) => `${encode(key)}=${encode(query[key])}`).join('&');
    const url = querystring.length ? `${location}?${querystring}` : location;
    return wxp[type]({ url, events }).catch(() => wx.redirectTo({ url }));
};
export const $router = {
    navigate: (location, query, events) => router('navigateTo', location, query, events),
    redirect: (location, query) => router('redirectTo', location, query),
    switchTab: (location) => router('switchTab', location),
    back: (option) => wxp.navigateBack(option),
};
